package com.schoolvisitors.controller.user;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.schoolvisitors.context.UserContext;
import com.schoolvisitors.dto.GetCubicleTimeSlotDto;
import com.schoolvisitors.dto.GetInstrumentTimeSlotDto;
import com.schoolvisitors.dto.ReservationCubicleDto;
import com.schoolvisitors.dto.ReservationInstrumentDto;
import com.schoolvisitors.entity.*;
import com.schoolvisitors.enums.PermissionEnum;
import com.schoolvisitors.response.ResultBody;
import com.schoolvisitors.service.AdminPermissionService;
import com.schoolvisitors.service.InstrumentService;
import com.schoolvisitors.service.TimeSlotService;
import io.lettuce.core.dynamic.annotation.Param;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/user")
public class InstrumentController {

    private final InstrumentService instrumentService;
    private final TimeSlotService timeSlotService;
    private final AdminPermissionService adminPermissionService;

    public InstrumentController(InstrumentService instrumentService, TimeSlotService timeSlotService, AdminPermissionService adminPermissionService) {
        this.instrumentService = instrumentService;
        this.timeSlotService = timeSlotService;
        this.adminPermissionService = adminPermissionService;
    }

    @GetMapping("/getInstruments")
    public ResultBody getInstruments(){
        return ResultBody.success(instrumentService.getInstrumentsByGroupJson());
    }

    @GetMapping("/getInstrumentInfo")
    public ResultBody getInstrumentInfo(@Param("instrumentId") Long instrumentId) {
        User user = UserContext.getUser();
        JSONObject result = new JSONObject();

        // 获取仪器信息
        Instrument instrument = instrumentService.getInstrumentById(instrumentId);
        result.put("instrument", instrument);

        // 获取有权限的审核人
        List<Admin> reviewers = adminPermissionService.getReviewers(instrument, PermissionEnum.INSTRUMENT_REVIEW);

        // 构造审核人列表返回
        List<Map<String, String>> reviewerList = new ArrayList<>();
        for (Admin admin : reviewers) {
            Map<String, String> reviewerMap = new HashMap<>();
            reviewerMap.put("id", String.valueOf(admin.getId()));
            reviewerMap.put("name", admin.getRealName());
            reviewerList.add(reviewerMap);
        }

        result.put("reviewer", reviewerList);

        return ResultBody.success(result);
    }


    @PostMapping("/getInstrumentTimeSlots")
    public ResultBody getInstrumentTimeSlots(@RequestBody @Valid GetInstrumentTimeSlotDto dto) {
        User user = UserContext.getUser();
        JSONArray data = instrumentService.getTimeSlotsVo(user.getId(), dto.getInstrumentId(), dto.getDate());
        return ResultBody.success(data);
    }
    @PostMapping("/createInstrumentReservation")
    public ResultBody createInstrumentReservation(@RequestBody @Valid ReservationInstrumentDto dto) {
        User user = UserContext.getUser();
        instrumentService.reservationInstrument(user, dto);
        return ResultBody.success();
    }

}
